package com.tom.architect02.p4.day38.simple7.base;

import android.content.Context;
import android.view.ViewGroup;

import com.tom.architect02.p4.day38.simple7.proxy.ViewGroupMvpProxy;
import com.tom.architect02.p4.day38.simple7.proxy.ViewGroupMvpProxyImpl;

/**
 * Added by Tom on 2024/01/31.
 */

public abstract class BaseMvpViewGroup<P extends BasePresenter> extends ViewGroup implements BaseView {
    private P mPresenter; // 主的Presenter。处理一对一的情况(一个View 仅对应一个Presenter)
    private ViewGroupMvpProxy mMvpProxy;

    public BaseMvpViewGroup(Context context) {
        super(context);
        // 创建P, 交给子类具体去时间。
        mPresenter = createPresenter();
        mPresenter.attach(this); // 绑定
        mMvpProxy = createMvpProxy();
        mMvpProxy.bindAndCreatePresenter(); // 执行注入

        initView();
        initData();
    }

    // 创建mvp 的代理，TODO：自己去模仿写 BaseFragment，ViewGroup的。
    protected ViewGroupMvpProxy createMvpProxy() {
        if (mMvpProxy == null) {
            mMvpProxy = new ViewGroupMvpProxyImpl<>(this);
        }
        return mMvpProxy;
    }

    // 子类去实现，每个ViewGroup 的实现不同。
    protected abstract P createPresenter();

    public P getPresenter() {
        return mPresenter;
    }

    protected abstract void initView();

    protected abstract void initData();

    protected abstract void setContentView();

    public void onUnbind(){
        mMvpProxy.unbindPresenter(); // 解绑presenter
        mPresenter.detach();
    }

}



